import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 先把 VueRouter 原型对象的 push 保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写 push | replace
// 第一个参数 location：告诉原来的 push 方法，传递哪些参数
// 第二个参数：成功的回调
// 第三个参数：失败的回调
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/index.vue')
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('@/views/list/index.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    component: () => import('@/views/cart/index.vue')
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('@/views/my/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/pwdlogin',
    name: 'pwdLogin',
    component: () => import('@/views/login/components/PwdLogin.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/login/components/Register.vue')
  },
  {
    path: '/recovery',
    name: 'recovery',
    component: () => import('@/views/recovery/index.vue'),
    children: [
      {
        path: '/',
        name: 'recoveryCert',
        component: () => import('@/views/recovery/recovery-cert.vue'),
      },
      {
        path: 'pwd',
        name: 'recoveryPwd',
        component: () => import('@/views/recovery/recovery-pwd.vue'),
      }
    ]
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/search/index.vue'),
    children: [
      {
        path: '/',
        name: 'searchIndex',
        component: () => import('@/views/search/search-index.vue'),
      },
      {
        path: 'list',
        name: 'searchList',
        component: () => import('@/views/search-list/index.vue'),
      }
    ]
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('@/views/detail/index.vue'),
    meta: { keepAlive: true }
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  next()
})

export default router
